/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Airplane;
import edu.chl.da.ejb.core.Seat;
import edu.chl.da.ejb.ctrl.IManageAirplane;
import edu.chl.da.ejb.ctrl.IManageSeat;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 * Controller bean managing listing, getting and removal events of airplanes
 * @author Max
 */
@ManagedBean(name = "ManageAirplaneController")
@SessionScoped
public class ManageAirplaneController {

    Airplane selectedAirplane;

    @EJB
    IManageAirplane ima;

    @EJB
    IManageSeat ims;

    public ManageAirplaneController() {
    
    }

    public String deleteAirplane() {
        Airplane a = ima.getAirplaneByID(selectedAirplane.getId());
        List<Seat> seatList = ims.getSeatList(a);
        for(Seat s : seatList){
            ims.removeSeat(s);
        }
        if(ima.removeAirplane(a)) {
            return "/admin/manageAirplanes.jsf?faces-redirect=true";
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Gick ej att radera flygplan, kontrollera eventuella flygningar"));
        return null;
    }

    public Airplane getSelectedAirplane() {
        return selectedAirplane;
    }

    public void setSelectedAirplane(Airplane selectedAirplane) {
        this.selectedAirplane = selectedAirplane;
    }

}
